<?php
/*
company:tiesh.com
project:common
function: 用户

created by: Bach
created date: 2016-02-23
description:

modification v1.01
modified by:
description:
*/
namespace app\hongyanche\controller;
class User
{
    private function vcode(){
        $vcode_times = (int)session('vcode_times');
        if($vcode_times<2){
            session('vcode_times',$vcode_times+1);
            return true;
        }
        if(session('vcode')){
            return true;
        }
        $vcode = input("vcode");
        if(!$vcode){
            return false;
        }
        $res = vcodeCheck($vcode);
        if(!$res['state']){
            return false;
        }
        session('vcode',1);
        return true;
    }
    function info(){
        $user = model('User');
        $user->refresh();
        $info = $user->info();
        if(isset($info['state']) && $info['state']){
            $data = $info['data'];
            unset($info['data']);
            $user_id = $data['user_id'];
            $member = model('member')->getInfoByIdUserId($user_id);
            if(isset($member['state']) && $member['state']){
                $data['member'] = $member['data'];
            }
            // dump($balance);
            $info['data']['info'] = $data;
            $info['data']['info']['session'] = session_id();
            $info['data']['info']['addr'][] = $info['data']['info']['province'];
            $info['data']['info']['addr'][] = $info['data']['info']['city'];
            $o = '';
            for ($i = 0;$i<4-strlen($info['data']['info']['user_id']);$i++){
                $o .= 0;
            }
            $info['data']['info']['invite_code'] = $o.$info['data']['info']['user_id'];
            // dump($member);
        }
        return json($info);
    }
    function modify(){
        $key = input('key');
        $value = input('value');
        return json(model('user')->modify([$key=>$value]));
    }
    function password_set(){
        $vcode = $this->vcode();
        if(!$vcode) return json(["errormsg"=>"vcode error"]);
        $password = input('password');
        $password_old = input('password_old');
        $res = model('user')->password_set(['password'=>$password,'password_old'=>$password_old]);
        if(isset($res['state']) && $res['state']) session('vcode',null);
        return json($res);
    }
    function password_reset(){
        $code = input('code');
        $tel = input('tel');
        if($code){
            $password = input('password');
            if(!preg_match('/((?=.*\d)(?=.*\D)|(?=.*[a-zA-Z])(?=.*[^a-zA-Z]))^.{8,16}/', $password, $matches)){
                return json(['state'=>0,'errormsg'=>'密码有误,格式为字母和数字8-16位组合']);
            }
            $res = model('user')->password_reset(['code'=>$code,'password'=>$password,'tel'=>$tel]);
            return json($res);
        }
        $vcode = $this->vcode();
        if(!$vcode) return json(["errormsg"=>"vcode error"]);
        $res = model('user')->password_reset_send($tel);
        if(isset($res['state']) && $res['state']) session('vcode',null);
        //获取tel
        $res['tel'] = $tel;
        dump($tel);
        if(!$tel){
            $user_id = model('user')->getUserId();
            $tel = db('user_tel')->where("user_id = $user_id and state = 1")->find();
            $res['tel'] = $tel['tel'];
        }

        return json($res);
    }
    //用户删除订单
    public function order_remove()
    {
        $order_id = input('order_id');
        $res = model('order')->cancel($order_id);
        return json($res);
    }
    //balance 查询账户余额
    public function balance(){
        $user_id = model('User')->getUserId();
        if(!$user_id){
            return ['state'=>0,'errormsg'=>'user_id missing'];
        }
        $res = model('Account')->getBalanceByUserId($user_id);
        if($res['state']){
            $res['data']['info']['balance'] = $res['data']['balance'];
            unset($res['data']['balance']);
        }
        return json($res);
    }
    public function recharge_order_create(){
        $total_fee = input('total_fee');
        $res = model('client')->orderCreateByUser($total_fee);
        return json($res);
    }

    //添加证件照
    public function photo_add()
    {
        $url = input('url');
        $type = input('type');
        $res = model('user')->photo_add($url,$type);
        return json($res);

    }
    //删除证件照
    public function photo_remove()
    {
        $photo_id = input('photo_id');
        $res = model('user')->photo_remove($photo_id);
        return json($res);
    }
    //证件照信息
    public function photo_info()
    {
        $res = model('user')->photo_info();
        return json($res);
    }
}
